package Test8;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket1 implements Runnable {
	// 共100票
	int ticket = 100;
	//获取锁对象
	Lock lo=new ReentrantLock();

	@Override
	public void run() {
		//模拟卖票
		while(true){
			
		if(ticket==0) {
			System.out.println("票卖完了");
			break;
		}
		lo.lock();
			if (ticket > 0) {
				//模拟选坐的操作
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + "正在卖票:" + ticket--);
			}
			lo.unlock();
		}
	}

}
